# == Schema Information
#
# Table name: questions
#
#  id             :integer(4)      not null, primary key
#  content        :text            default(""), not null
#  position       :integer(1)      default(1)
#  day            :integer(4)
#  answer_1       :string(255)
#  answer_2       :string(255)
#  answer_3       :string(255)
#  correct_answer :integer(1)      default(1)
#  created_at     :datetime
#  updated_at     :datetime
#
# Indexes
#
#  index_questions_on_day  (day)
#

class Question < ActiveRecord::Base
  validates :content, :day, :answer_1, :answer_2, :answer_3, :correct_answer, :presence => true  
    
  has_many :answers, :dependent => :destroy
  
  attr_accessible :content, :day, :answer_1, :answer_2, :answer_3, :correct_answer, :position
  
  scope :visible,  where(["day <= ?", Date.today.day])
  
  def self.actual(person)
    _ids = person.questions.select("#{quoted_table_name}.id").map{|q| q.id}
    where(["day <= ? AND id NOT IN (?)", Date.today.day, _ids.blank? ? 0 : _ids]).order('position')
  end
  
end
